import { message } from 'antd';
import {produce} from 'immer';
import { reqAddSudent, reqFetchList, reqUpdateStudent, reqUploadFile} from './server';

export const models = {
  name: 'managementStudent',
  model: {
    state:{
      loading: false,
      studentList: [],
      list: [1,2,3,4,5,6]
    },
    reducers: {
      setLoading(state, payload) {
        return produce(state, draft => {
          draft.loading = payload
        })
      },
      setStudentList(state, payload) {
        return produce(state, draft => {
          draft.studentList = payload
        })
      },
      set(state,payload){
        return{...state,...payload}
      }
    },
    effects: dispatch => {
      return {
        async fetch(params) {
          // 请求学生列表
          dispatch.managementStudent.setLoading(true)
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.managementStudent.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.managementStudent.setLoading(false)
          }
          
          // 
          // dispatch.managementStudent.setState({
          //   loading: true
          // })
        },
        async delete(params) {
          // dispatch.managementStudent.setLoading(true)

          // 删除学生
          
          
          // dispatch.managementStudent.fetch()
          dispatch.managementStudent.setState()
          
        },
        async add(params) {
          //增加学生
          dispatch.managementStudent.setLoading(true)

          
          const {res, code, msg} = await reqAddSudent(params);
          
          // 
          // dispatch.managementStudent.fetch()
          code !== 0 ? await message.error('添加失败' + msg) : await message.success('添加成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.managementStudent.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.managementStudent.setLoading(false)
          }

        },
        async update(params,a) {
          // 编辑更新
          
          dispatch.managementStudent.setLoading(true)
          const {code, data, msg} = await reqUpdateStudent(params);
          // code === 0 && dispatch.managementStudent.fetch()
          
          
          code !== 0 ? await message.error('修改失败' + msg) : await message.success('修改成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.managementStudent.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.managementStudent.setLoading(false)
          }
        },
        async uploadFile(params){
          
          dispatch.managementStudent.setLoading(true)
          const {code, data, msg} = await reqUploadFile(params);
          code !== 0 ? await message.error('上传失败' + msg) : await message.success('上传成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.managementStudent.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.managementStudent.setLoading(false)
          }
        }
      }
    }
  }
}